package com.yunshang.shoppingmall.shopping.Finace;

import com.yunshang.shoppingmall.entity.FinaceToData;
import com.yunshang.shoppingmall.entity.FinaceTotal;
import com.yunshang.shoppingmall.entity.Hsys.JsonBack;
import com.yunshang.shoppingmall.entity.Hsys.JsonBackData;
import com.yunshang.shoppingmall.entity.UserNo;
import com.yunshang.shoppingmall.service.Finace.FinaceService;
import com.yunshang.shoppingmall.service.Finace.FinaceTotalService;
import com.yunshang.shoppingmall.service.SysUserNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("/wap")
@Controller
public class FinaceHsController {

    @Autowired
    FinaceService finaceService;

    @Autowired
    SysUserNoService sysUserNoService;

    @Autowired
    FinaceTotalService finaceTotalService;

    @ResponseBody
    @RequestMapping("/hsFinace")
    public JsonBack FinaceAdd(HttpServletRequest request) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dtTime = df.format(new Date());

        try {
            String hsUserNo = request.getParameter("UserNo");
            if (hsUserNo == null || hsUserNo == "" || hsUserNo.isEmpty()) {
                return new JsonBack(false, "编号错误!", null, dtTime);
            }
            String Amount1 = request.getParameter("Amount");
            double Amount = Double.parseDouble(Amount1);

            if (Amount <= 0) {
                return new JsonBack(false, "参数错误!", null, dtTime);
            }

            String AmountType1 = request.getParameter("AmountType");
            int AmountType = Integer.parseInt(AmountType1);
            if(AmountType==5)
            {
                AmountType=1;
            }
            if (AmountType != 1 && AmountType != 2) {
                return new JsonBack(false, "类型错误!", null, dtTime);
            }

            String EncryptStr = request.getParameter("EncryptStr");
            if (!EncryptStr.equals("60f5a3c2ab917609282ebaafc6365589")) {
                return new JsonBack(false, "非法操作!", null, dtTime);
            }

            //查询编号
            UserNo userNo = sysUserNoService.selectUserNoByUserNo(hsUserNo);
            if (userNo != null) {
                FinaceToData finaceToData = null;

                //充值
                FinaceTotal finaceTotal2 = finaceTotalService.selectFinaceTotal(userNo.getId(), AmountType + "");

                if (finaceTotal2 == null) {//用户第一次充值
                    FinaceTotal finaceTotal3 = new FinaceTotal();
                    finaceTotal3.setBalance(Amount);
                    finaceTotal3.setUserId(userNo.getId());
                    finaceTotal3.setfType(AmountType + "");
                    finaceTotal3.setChanggeTime(new Timestamp(System.currentTimeMillis()));
                    finaceTotal3.setOpratorId(-1);
                    finaceTotal3.setAmount(Amount);
                    finaceTotal3.setOpratorUId(userNo.getId());
                    int firstRow = finaceTotalService.insertSelective(finaceTotal3);

                    //积分表中记录明细
                    finaceToData = new FinaceToData();
                    finaceToData.setClassify(AmountType);
                    finaceToData.setAmount(new BigDecimal(Amount));
                    finaceToData.setUserId(userNo.getId());
                    finaceToData.setBalance(new BigDecimal(Amount));
                    finaceToData.setFinaceTime(new Timestamp(System.currentTimeMillis()));
                    finaceToData.setFinaceType("115");
                    finaceToData.setOpratorUId(userNo.getId());
                    finaceToData.setOpratorId(-1);
                    finaceToData.setRemark("转账hs");
                    int row1 = finaceService.insertFinaceData(finaceToData);

                    if (firstRow > 0 && row1 > 0) {
                        return new JsonBack(true, "操作成功!", new JsonBackData(hsUserNo,Amount), dtTime);
                    } else {
                        return new JsonBack(false, "充值失败!", null, dtTime);
                    }
                }

                double balance2 = finaceTotal2.getBalance();//用户的余额
                int fId = finaceTotal2.getfId();

                FinaceTotal finaceTotal = new FinaceTotal();
                finaceTotal.setUserId(userNo.getId());
                finaceTotal.setfType(AmountType + "");
                finaceTotal.setBalance(balance2 + Amount);
                finaceTotal.setfId(fId);
                int row = finaceTotalService.updateByPrimaryKeySelective(finaceTotal);

                if (row > 0) {//充值成功
                    finaceToData = new FinaceToData();
                    finaceToData.setClassify(AmountType);
                    finaceToData.setAmount(new BigDecimal(Amount));
                    finaceToData.setUserId(userNo.getId());
                    finaceToData.setBalance(new BigDecimal(balance2 + Amount));
                    finaceToData.setFinaceTime(new Timestamp(System.currentTimeMillis()));
                    finaceToData.setRemark("转账hs");
                    finaceToData.setFinaceType("115");
                    finaceToData.setOpratorUId(userNo.getId());
                    finaceToData.setOpratorId(-1);

                }
                int row1 = finaceService.insertFinaceData(finaceToData);
                if (row > 0 && row1 > 0) {
                    return new JsonBack(true, "操作成功!", new JsonBackData(hsUserNo,Amount), dtTime);
                } else {
                    return new JsonBack(false, "充值失败!", null, dtTime);
                }
            } else {
                return new JsonBack(false, "编号不存在!", null, dtTime);
            }
        } catch (Exception e) {
            return new JsonBack(false, "操作失败!", null, dtTime);
        }
    }

}
